package com.tbit.uqbike.client.util;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 微信输出
 *
 * @author Leon
 * 2014-7-3 上午11:17:52
 */
@Slf4j
public class WeixinOutputUtil {
    /**
     * 输出指定类型字符串
     */
    public static void render(HttpServletResponse response, String text, String contentType) {
        try {
            response.setContentType(contentType);
            response.getWriter().write(text);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 直接输出纯字符串.
     */
    public static void renderText(HttpServletResponse response, String text) {
        render(response, text, "text/plain;charset=UTF-8");
    }

    /**
     * 向请求端发送返回数据(微信)
     */
    public static void print(HttpServletResponse response, String content) {
        try {
            response.setContentType("text/xml");
            response.setCharacterEncoding("utf-8");
            PrintWriter pw = response.getWriter();
            pw.print(content);
            pw.flush();
            pw.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
